package com.example.intenttest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    
    private RadioGroup rg;  // 单选框
    private Button btn; // 按钮
    private ImageView imageView;  // 图片
    private String type;  // 用于存储选中的值

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    
    // 初始化组件, 并添加相应的监听器
    private void init() {
        rg = (RadioGroup) findViewById(R.id.rg);
        btn = (Button) findViewById(R.id.btn);
        imageView = (ImageView) findViewById(R.id.my_image); 
        // 注册单选框的监听事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                type = "";
                if (i == R.id.rb_one) {
                    type = "晴天";
                }
                if (i == R.id.rb_two) {
                    type = "爱心";
                }
            }
        });
        // 注册按钮监听事件
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type != null && !"".equals(type)) {
                    Intent intent = new Intent();
                    switch (type) {
                        case "晴天":
                            intent.putExtra("imageResource", R.drawable.sun);
                            break;
                        case "爱心":
                            intent.putExtra("imageResource", R.drawable.luckey);
                            break;
                    }
                    intent.setClass(MainActivity.this, MyActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(MainActivity.this, "请选择至少一项！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}